//
//  OJIssueViewController.m
//  openJira
//
//  Created by Vincent Demay on 30/04/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJIssueViewController.h"
#import "OJConnector.h"
#import "OJComment.h"


@implementation OJIssueViewController

@synthesize issue, comments;

- (id) init {
	self = [super init];
	if (self != nil) {
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minilogo.png"]];
		self.navigationItem.titleView = imageView;
		[imageView release];
		
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"comment" style:UIBarButtonItemStyleBordered target:self action:@selector(onCommentTouch:)];
	}
	return self;
}

- (id) initWithIssue:(OJIssue*) _issue {
	self = [self init];
	if (self != nil) {
		issue = [_issue retain]; 
	}
	return self;
}

- (void) onCommentTouch:(id) sender {
	[post release];
	post = [[TTPostController alloc] init];
	[post showInView:self.view animated:YES];
	post.delegate = self;
	post.originView.backgroundColor = BG_COLOR;
}

- (void)loadView {
	[super loadView];
	CGRect frame = self.view.frame;
	
	scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 40)];
	[self.view addSubview:scrollView];
	contentView = [[UIView alloc] init];
	[scrollView addSubview:contentView];
	
	TTView* info = [[TTView alloc] init];
	info.style = [TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:10] next:
				  [TTSolidFillStyle styleWithColor:CLEAR_BLUE next:
				   [TTSolidBorderStyle  styleWithColor:GREY_COLOR width:0.0 next:nil]]];
	info.backgroundColor = [UIColor clearColor];
	[contentView addSubview:info];
	
	//TOP : priority and key
	TTImageView * image = [[TTImageView alloc] init];
	image.frame = CGRectMake(10, 10, 15, 15);
	image.urlPath = issue.priority.icon;
	[contentView addSubview:image];
	
	UILabel* key = [[UILabel alloc] init];
	key.backgroundColor = [UIColor clearColor];
	key.font = [UIFont boldSystemFontOfSize:16];
	key.textColor = BG_COLOR;	
	key.frame = CGRectMake(30, 10, frame.size.width - 40, 15);
	key.text = issue.key;
	[contentView addSubview:key];
	
	//TOP right : status
	TTImageView *status = [[TTImageView alloc] init];
	status.frame = CGRectMake(frame.size.width - 25, 10, 15, 15);
	status.urlPath = issue.status.icon;
	[contentView addSubview:status];
	
	UILabel *statusText = [[UILabel alloc] init];
	statusText.frame = CGRectMake(frame.size.width - 30 - 150, 10, 150, 15);
	statusText.font = [UIFont boldSystemFontOfSize:12];
	statusText.textAlignment = UITextAlignmentRight;
	statusText.backgroundColor = [UIColor clearColor];
	statusText.textColor = GREY_COLOR;
	statusText.text = issue.status.name;
	[contentView addSubview:statusText];
	
	// summary
	UILabel *summary = [[UILabel alloc] init];
	summary.textColor = GREY_COLOR;
	summary.font = [UIFont boldSystemFontOfSize:16];
	summary.backgroundColor = [UIColor clearColor];
	summary.numberOfLines = 0;
	CGSize size = [issue.summary sizeWithFont:summary.font constrainedToSize:CGSizeMake(frame.size.width - 20, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
	summary.frame = CGRectMake(10, key.bottom + 3, frame.size.width - 20, size.height);
	summary.text = issue.summary;
	[contentView addSubview:summary];
	
	//Dates
	UILabel *created = [[UILabel alloc] init];
	created.textColor = GREY_COLOR;
	created.backgroundColor = [UIColor clearColor];
	created.font = [UIFont systemFontOfSize:12];
	created.frame = CGRectMake(10, summary.bottom + 5, 250, 10);
	created.text = [NSString stringWithFormat:@"created: %@", issue.created];
	[contentView addSubview:created];
	
	UILabel *modified = [[UILabel alloc] init];
	modified.textColor = GREY_COLOR;
	modified.backgroundColor = [UIColor clearColor];
	modified.font = [UIFont systemFontOfSize:12];
	modified.frame = CGRectMake(10, created.bottom + 5, 250, 10);
	modified.text = [NSString stringWithFormat:@"updated: %@", issue.updated];
	[contentView addSubview:modified];
	
	UILabel *assigne = [[UILabel alloc] init];
	assigne.textColor = GREY_COLOR;
	assigne.font = [UIFont systemFontOfSize:12];
	assigne.backgroundColor = [UIColor clearColor];
	assigne.frame = CGRectMake(10, modified.bottom + 10, frame.size.width, 15);
	assigne.text = [NSString stringWithFormat:@"Assigne: %@", issue.assigne];
	[contentView addSubview:assigne];
	
	UILabel *reporter = [[UILabel alloc] init];
	reporter.textColor = GREY_COLOR;
	reporter.font = [UIFont systemFontOfSize:12];
	reporter.backgroundColor = [UIColor clearColor];
	reporter.frame = CGRectMake(10, assigne.bottom + 5, frame.size.width, 15);
	reporter.text = [NSString stringWithFormat:@"Reporter: %@", issue.reporter];
	[contentView addSubview:reporter];
	
	
	//Compute info size
	info.frame = CGRectMake(5, 5, frame.size.width - 10 , reporter.bottom);
	
	/////////////////////
	//START DESCRIPTION//
	/////////////////////
	
	UILabel *descriptionHeader = [[UILabel alloc] init];
	descriptionHeader.textColor = [UIColor whiteColor];
	descriptionHeader.font = [UIFont boldSystemFontOfSize:14];
	descriptionHeader.textAlignment = UITextAlignmentCenter;
	descriptionHeader.backgroundColor = BG_COLOR;
	descriptionHeader.frame = CGRectMake(0, reporter.bottom + 15, frame.size.width, 20);
	descriptionHeader.text = @"Description";
	[contentView addSubview:descriptionHeader];
	
	//description
	UILabel *description = [[UILabel alloc] init];
	description.textColor = [UIColor blackColor];
	description.font = [UIFont boldSystemFontOfSize:14];
	description.backgroundColor = [UIColor clearColor];
	description.numberOfLines = 0;
	CGSize descsize = [issue.description sizeWithFont:description.font constrainedToSize:CGSizeMake(frame.size.width - 20, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
	description.frame = CGRectMake(10, descriptionHeader.bottom + 5, frame.size.width - 20, descsize.height);
	description.text = issue.description;
	[contentView addSubview:description];
	
	//////////////////
	//START COMMENTS//
	//////////////////
	
	UILabel *commentsHeader = [[UILabel alloc] init];
	commentsHeader.textColor = [UIColor whiteColor];
	commentsHeader.font = [UIFont boldSystemFontOfSize:14];
	commentsHeader.textAlignment = UITextAlignmentCenter;
	commentsHeader.backgroundColor = BG_COLOR;
	commentsHeader.frame = CGRectMake(0, description.bottom + 10, frame.size.width, 20);
	commentsHeader.text = @"Comments";
	[contentView addSubview:commentsHeader];
	
	commentLoading = [[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleWhiteBox];
	commentLoading.text = @"Loading comments...";
	[commentLoading sizeToFit];
	commentLoading.frame = CGRectMake(0, commentsHeader.bottom + 5, self.view.width, commentLoading.height);
	[self.view addSubview:commentLoading];
	[commentLoading setIsAnimating:YES];
	
	//////////////////////////////////
	// releases and compute sizes   //
	//////////////////////////////////
	
	//compute UIScrollViewSize
	contentView.frame = CGRectMake(0, 0, frame.size.width, commentLoading.bottom);
	scrollView.contentSize = CGSizeMake(frame.size.width, commentLoading.bottom);
	
	scrollContentHeight = commentsHeader.bottom;
	
	///////////////////////////////////
	//       Activity label          //
	///////////////////////////////////
	
	//release
	[image release];
	[key release];
	[status release];
	[statusText release];
	[summary release];
	[created release];
	[modified release];
	[descriptionHeader release];
	[description release];
	[assigne release];
	[reporter release];
	[commentsHeader release];
	
	//////////////////////////////
	// ask for comment retreive //
	//////////////////////////////
	
	comments = [[OJCommentList alloc] init];
	comments.delegate = self;
	[[OJConnector getDefault] getComments:comments forIssue:issue];
	
}


- (void) addAComment:(OJComment*) currentComment {
	
	CGRect frame = self.view.frame;
	
	UILabel *author = [[UILabel alloc] init];
	author.textColor = [UIColor blackColor];
	author.font = [UIFont boldSystemFontOfSize:14];
	author.backgroundColor = [UIColor clearColor];
	author.frame = CGRectMake(10, scrollContentHeight + 10, 150, 15);
	author.text = currentComment.author;
	[contentView addSubview:author];
	
	UILabel *modified = [[UILabel alloc] init];
	modified.textColor = GREY_COLOR;
	modified.font = [UIFont boldSystemFontOfSize:14];
	modified.backgroundColor = [UIColor clearColor];
	modified.textAlignment = UITextAlignmentRight;
	modified.frame = CGRectMake(160, scrollContentHeight + 10, frame.size.width - 170, 15);
	modified.text = currentComment.updated;
	[contentView addSubview:modified];
	
	UILabel *commentBody = [[UILabel alloc] init];
	commentBody.textColor = GREY_COLOR;
	commentBody.font = [UIFont systemFontOfSize:14];
	commentBody.backgroundColor = [UIColor clearColor];
	commentBody.numberOfLines = 0;
	CGSize size = [currentComment.body sizeWithFont:commentBody.font constrainedToSize:CGSizeMake(frame.size.width - 20, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
	commentBody.frame = CGRectMake(10, scrollContentHeight + 25, frame.size.width - 20, size.height);
	commentBody.text = currentComment.body;
	[contentView addSubview:commentBody];
	
	scrollContentHeight = commentBody.bottom;
	[commentBody release];
	[modified release];
	[author release];
	
	contentView.frame = CGRectMake(0, 0, frame.size.width, commentBody.bottom);
	scrollView.contentSize = CGSizeMake(frame.size.width, commentBody.bottom);
}


///////////////// SEND RESULTS //////////////////

- (void) object:(OJCommentList*) _comments isReady:(BOOL) onError {
	
	[commentLoading setIsAnimating:NO];
	commentLoading.hidden = YES;
	
	//get comments for this issue
	for (int i=0; i<[_comments.comments count]; i++) {
		OJComment *currentComment = (OJComment*)[_comments.comments objectAtIndex:i];
	
		[self addAComment:currentComment];
	}
}

- (void) object:(OJComment*) comment isSent:(BOOL) onError {
	if (!onError) {
		//ok comment is posted
		[post dismissPopupViewControllerAnimated:YES];
		[self addAComment:comment];
	} else {
		//FIXME 
		// send an error to user
	}
}


- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}


- (void)dealloc {
	[comments release];
	
	[contentView release];
	[scrollView release];
	[post release];
	
    [super dealloc];
}


//////////////////// TTPOSTCONTROLLERDELEGATE /////////////////
- (BOOL)postController:(TTPostController*)postController willPostText:(NSString*)text {
	
	TTActivityLabel *activity = [[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleBlackBezel];
	activity.text = @"Sending...";
	[activity sizeToFit];
	activity.frame = CGRectMake(0, 100, self.view.width, activity.height);
	[self.view addSubview:activity];
	[activity setIsAnimating:YES];
	[postController.view addSubview:activity];
	[activity release];
	
	OJComment * comment = [[OJComment alloc] init];
	comment.delegate = self;
	comment.body = text;
	[comment addToIssue:issue];
	return NO;
}

@end
